package com.motu.vertx.module.utility.webhandler;

import com.motu.vertx.module.utility.webtool.BaseWebHandler;
import com.motu.vertx.module.utility.webtool.SearchCondition;

import java.util.ArrayList;
import java.util.List;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.WebClient;
import io.vertx.sqlclient.Pool;

public class BadwordsHandler extends BaseWebHandler{
	private static final String tableName1 = "badwords";
	private static Pool jdbcClientBadword = null;

	public static void init(Pool jdbcBadword){
		jdbcClientBadword = jdbcBadword;
	}
	public static void add(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		String tableName = tableName1;
		JsonObject pdata = routingContext.body().asJsonObject().getJsonObject("pdata");
		if (json.containsKey("tbname")) {  //某些项目独立的敏感库，如乱3 badwords_ldt3
			tableName = json.getString("tbname");
			pdata.remove("tbname");
		}
		addToTable(routingContext,jdbcClientBadword, tableName, pdata);
	}

	public static void  update(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		String tableName = tableName1;
		JsonObject pdata = routingContext.body().asJsonObject().getJsonObject("pdata");
		if (json.containsKey("tbname")) {  //某些项目独立的敏感库，如乱3 badwords_ldt3
			tableName = json.getString("tbname");
			pdata.remove("tbname");
		}
		updateToTable(routingContext,jdbcClientBadword, tableName, pdata);
	}

	public static void  getone(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		String tableName = tableName1;
		if (json.containsKey("tbname")) {  //某些项目独立的敏感库，如乱3 badwords_ldt3
			tableName = json.getString("tbname");
		}
		getFromTable(routingContext,jdbcClientBadword, tableName);

	}
	public static void  delete(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		String tableName = tableName1;
		if (json.containsKey("tbname")) {  //某些项目独立的敏感库，如乱3 badwords_ldt3
			tableName = json.getString("tbname");
		}
		deleteFromTable(routingContext,jdbcClientBadword, tableName);
	}
	public static void  search(RoutingContext routingContext,JsonObject json,String adminName,WebClient client){
		String tableName = tableName1;
		if (json.containsKey("tbname")) {  //某些项目独立的敏感库，如乱3 badwords_ldt3
			tableName = json.getString("tbname");
		}
		List<SearchCondition> scList = new ArrayList<SearchCondition>();
		scList.add(new SearchCondition("word", "like", null, null));
		
		searchFromTable(routingContext, jdbcClientBadword, json, tableName, scList);
	}
	

}
